package q2;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerHttpQ2 extends Thread
{
	private static final String STR_REQUEST = "GET";
	
	protected Socket socketIn = null;

	public ServerHttpQ2( Socket sktIn )
	{
		socketIn = sktIn;
		
		//	Start the thread
		start();
	}
	
	public void run()
	{
		BufferedReader in = null;
		PrintWriter out = null;
		
		boolean bIsDone = false;
		
		try
		{
			in = new BufferedReader( new InputStreamReader( socketIn.getInputStream()) );
			out = new PrintWriter( socketIn.getOutputStream(), true );

			do
			{
				//	If a new request was received
				if( in.ready() == true )
				{
					System.out.println( "ServerHttpQ2 receiving...");
					
					String strReceivedRequest = "";
					String strLine = "";
					do
					{
						strReceivedRequest += strLine + "\n";
						strLine = in.readLine();
				
						//	DEBUG
						//	System.out.println( "READ Line :" + strLine + "-------" );
					} while( strLine.compareTo("") != 0 );
					
					System.out.println( "ServerHttpQ2 received request :\n" + strReceivedRequest); // Read one line and output
					
					//	Test for REQ substring
					String strCommandID = strReceivedRequest.substring(1, 4);
					//System.out.println( "DEBUG =" + strCommandID + "==" + STR_REQUEST + "     " + strCommandID.compareTo( STR_REQUEST ));
					
					if( strCommandID.compareTo( STR_REQUEST ) == 0 )
					{
						//	Find the requested file name string
						int iNextSpace = strReceivedRequest.indexOf(' ', 5);
						String strRequestedFile = strReceivedRequest.substring(5, iNextSpace + 1);

						//	DEBUG 
						//	System.out.println( "DEBUG req file = " + strRequestedFile );

						sendResponse( out, strRequestedFile );
					}
				}
				
				try
				{
					sleep( 100 );
				} catch( Exception e )
				{
					System.out.println( "ServerHttpQ2 error : " + e.toString() );
				}
				
				//	TODO find when to kill the thread
			} while( bIsDone == false );

			in.close();
			
		} catch(Exception e)
		{
			System.out.println( "ServerHttpQ2 error : " + e.toString() );
		}	
	}
	

	private void sendResponse( PrintWriter out, String strRequestedFile )
	{
		// System.out.println( "sendresponse" );

		String strResponse = "";
		
		//	Read requested file
		try
		{
			FileInputStream fstream = new FileInputStream( "." + strRequestedFile );
			// Get the object of DataInputStream
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			//Read File Line By Line
			while ((strLine = br.readLine()) != null)
			{
				strResponse += strLine;
			}
			//Close the input stream
			in.close();
		} catch (Exception e){//Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}
		
		
		//	Test attempt to respond to the client
		//	System.out.println( "DEBUG server sending " + strResponse ); 					
		out.println( strResponse );
		out.close();
	}	
}
